# -*- coding: utf-8 -*-
# בש״ד
#
# Teniente Django Template Slots - Part of the Cuba Libre Project
# Copyright (C) 2009, Joshua "jag" Ginsberg <jag@flowtheory.net>
# 
# Para mi amor, que inspira a mi cada respiración y que extraña a Cuba
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
from django.test import TestCase
from django.template import Template, RequestContext, TemplateSyntaxError
from django.http import HttpRequest, SimpleCookie
from django.conf import settings
from django.core.handlers.base import BaseHandler
from taino.api import Component
from teniente import api
from teniente.plugins import *

handler = BaseHandler()
handler.load_middleware()
environ = {
    'HTTP_COOKIE':       SimpleCookie(),
    'PATH_INFO':         '/',
    'QUERY_STRING':      '',
    'REMOTE_ADDR':       '127.0.0.1',
    'REQUEST_METHOD':    'GET',
    'SCRIPT_NAME':       '',
    'SERVER_NAME':       'testserver',
    'SERVER_PORT':       '80',
    'SERVER_PROTOCOL':   'HTTP/1.1',
}

class TestUserLink(Component):
    component_id = 'net.skwx.cubalibre.teniente.TestUserTool'
    component_description = 'Test user tool'
    implements = [api.ITemplateSlotToken,]
    
    def get_slot_id(self):
        return 'org.djangoproject.base.userlinks'
    
    def render_token(self, context):
        return u'<a href="http://starboard.flowtheory.net/">Starboard</a>'

class TestObjectTool(Component):
    component_id = 'net.skwx.cubalibre.teniente.TestObjectTool'
    component_description = 'Test object tool'
    implements = [api.ITemplateSlotToken,]
    weight = 20
    
    def get_slot_id(self):
        return 'org.djangoproject.base.object-tools'
    
    def render_token(self, context):
        return u'Object Tool'

class TestObjectToolToo(Component):
    component_id = 'net.skwx.cubalibre.teniente.TestObjectToolToo'
    component_description = 'Test object tool too'
    implements = [api.ITemplateSlotToken,]
    weight = 10
    
    def get_slot_id(self):
        return 'org.djangoproject.base.object-tools'
    
    def render_token(self, context):
        return u'Object Tool Too'

class TestObjectToolAlso(Component):
    component_id = 'net.skwx.cubalibre.teniente.TestObjectToolAlso'
    component_description = 'Test object tool also'
    implements = [api.ITemplateSlotToken,]
    weight = 30
    
    def get_slot_id(self):
        return 'org.djangoproject.base.object-tools'
    
    def render_token(self, context):
        return u'Object Tool Also'

class TestEmptySlot(Component):
    component_id = 'net.skwx.cubalibre.teniente.TestEmptySlot'
    component_description = 'Empty slot'
    implements = [api.ITemplateSlotToken,]
    
    def get_slot_id(self):
        return 'org.djangoproject.base.nav-global'
    
    def render_token(self, context):
        return u''

class TenienteTests(TestCase):
    
    def make_faux_request(self):
        req = HttpRequest()
        req.META = environ.copy()
        for middleware_method in handler._request_middleware:
            middleware_method(req)
        return req
    
    def test_bad_slot_id(self):
        t = Template('{% load slots %}{% define_slot "slot.does.not.exist" %}')
        ctx = RequestContext(self.make_faux_request(), {})
        self.assertRaises(TemplateSyntaxError,
                          t.render,
                          ctx)
    
    def test_one_token_slot(self):
        t = Template('''{% load slots %}
            {% define_slot org.djangoproject.base.userlinks %}'''
            )
        ctx = RequestContext(self.make_faux_request(), {})
        html = t.render(ctx)
        self.assertEqual('<a href="http://starboard.flowtheory.net/">Starboard</a>',
                         html.strip())
        
    def test_slot_weights(self):
        t = Template('''{% load slots %}
            {% define_slot org.djangoproject.base.object-tools %}'''
            )
        ctx = RequestContext(self.make_faux_request(), {})
        html = t.render(ctx)
        list_items = re.findall(r'<li>[A-Za-z ]+</li>', html)
        self.assertEqual(list_items[0], '<li>Object Tool Too</li>')
        self.assertEqual(list_items[1], '<li>Object Tool</li>')
        self.assertEqual(list_items[2], '<li>Object Tool Also</li>')
    
    def test_empty_slots_dropped(self):
        t = Template('''{% load slots %}
            {% define_slot org.djangoproject.base.nav-global %}''')
        ctx = RequestContext(self.make_faux_request(), {})
        html = t.render(ctx)
        self.assertEqual('', html.strip())
         